#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e3 + 10;
int n, m;
bool st[N];
int dist[N];
int edges[N][N];
void kck()
{
    memset(dist, 0x3f, sizeof(dist));
    memset(st, 0, sizeof(st));
    dist[1] = 0;
    for (int i = 1; i <= n; i++)
    {
        int a = 0;
        for (int j = 1; j <= n; j++)
        {
            if (!st[j] && dist[j] < dist[a])
            {
                a = j;
            }
        }
        st[a] = true;
        for (int k = 1; k <= n; k++)
        {
            int w = edges[a][k];
            if (w == 0x3f3f3f3f)
            {
                continue;
            }
            if (dist[a] + w < dist[k])
            {
                dist[k] = dist[a] + w;
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    memset(edges, 0x3f, sizeof(edges));
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edges[u][v] = min(w, edges[u][v]);
    }
    kck();
    int ret = 0;
    for (int i = 1; i <= n; i++)
    {
        ret += dist[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            swap(edges[i][j], edges[j][i]);
        }
    }
    kck();
    for (int i = 1; i <= n; i++)
    {
        ret += dist[i];
    }
    cout << ret << endl;
    return 0;
}